<?php
/**
 * Memcache
 */
class Mem
{
    private static $mcResource;

    public static function fetch($key)/*{{{*/
    {
        $mcHandle   =   self::mcHandle($key);
        if (empty($mcHandle))   return  FALSE;

        $return     =   $mcHandle->get($key);
        if (FALSE   === $return)
        {
            $return =   $mcHandle->get($key);
        }        
        
        return  $return;
    }/*}}}*/

    public static function save($key, $value, $expire = AC::DEFAULT_CACHE_TIME)/*{{{*/
    {
        $mcHandle   =   self::mcHandle($key);
        if (empty($mcHandle))   return  FALSE;

        $return     =   $mcHandle->set($key, $value, $expire);
        if (FALSE   === $return)
        {
            $return =   $mcHandle->set($key, $value, $expire);
        }
        return  $return;
    }/*}}}*/

    public static function add($key, $value, $expire = AC::DEFAULT_CACHE_TIME)/*{{{*/
    {
        $mcHandle   =   self::mcHandle($key);
        if (empty($mcHandle))   return  FALSE;

        $return     =   $mcHandle->add($key, $value, $expire);
        if (FALSE   === $return)
        {
            $return =   $mcHandle->add($key, $value, $expire);
        }
        return  $return;
    }/*}}}*/

    public static function remove($key)/*{{{*/
    {
        $mcHandle   =   self::mcHandle($key);
        if (empty($mcHandle))   return  FALSE;

        $return     =   $mcHandle->delete($key);
        if (FALSE   === $return)
        {
            $return =   $mcHandle->delete($key);
        }
        return  $return;
    }/*}}}*/

    public static function increment($key, $value)/*{{{*/
    {
        $mcHandle   =   self::mcHandle($key);
        if (empty($mcHandle))   return  FALSE;

        if (!is_numeric($value) || $value < 1)
        {
            return  FALSE;
        }

        $result =   $mcHandle->increment($key, $value);
        return  $result;
    }/*}}}*/

    public static function decrement($key, $value)/*{{{*/
    {
        $mcHandle   =   self::mcHandle($key);
        if (empty($mcHandle))   return  FALSE;

        if (!is_numeric($value) || $value < 1)
        {
            return  FALSE;
        }

        $result =   $mcHandle->decrement($key, $value);
        return  $result;
    }/*}}}*/

    public static function multiFetch($keys)/*{{{*/
    {
        if (empty($keys) || !is_array($keys))   return  array();

        $client =   array();
        foreach ($keys  as  $key)
        {
            $tmp=   AC::getMcClient($key);
            if (empty($client[$tmp['clientServer']]))   $client[$tmp['clientServer']]   =   array($key);
            else    $client[$tmp['clientServer']][]     =   $key;
        }

        $return =   array();
        foreach ($client    as  $server =>  $multiKeys)
        {
            $temp   =   self::mcHandle($multiKeys[0])->getMulti($multiKeys);
            if (empty($temp) || !is_array($temp))   continue;

            $return =   $return + $temp;
        }

        return  $return;
    }/*}}}*/

    private function mcHandle($key)/*{{{*/
    {
        $configArray    =   AC::getMcClient($key);
        if ('NoCache'   ==  $configArray)
        {
            # the Key don't use mc
            return  FALSE;
        }

        if (empty($configArray['serverStatus']))
        {
            exit ('Mc server is not work! ');
            return  FALSE;
        }
        if (empty(MemcacheMap::$mcHostConfig[$configArray['clientServer']]))
        {
            exit ('Mc server is not exist! ');
            return  FALSE;
        }
        $clientServer   =   $configArray['clientServer'];

        if (isset(self::$mcResource[$clientServer]) ||  is_resource(self::$mcResource[$clientServer]))
        {
            return  self::$mcResource[$clientServer];
        }

        $hostConfig =   MemcacheMap::$mcHostConfig[$configArray['clientServer']];

        extract($hostConfig);
        $mcHandle   =   new Memcached();
        $ret = $mcHandle->addServer($host, $port);
        self::$mcResource[$clientServer]    =   $mcHandle;

        return  $mcHandle;
    }/*}}}*/

}

